package org.game.module.fight;

import java.util.Date;
import java.util.Map;

import org.game.module.fight.handler.FightHandler;
import org.game.protobuf.s2c.S2CHeroMsg.HeroInfo;

import com.google.common.collect.Maps;
import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.log.LogFactory;
import com.xiaoleilu.hutool.util.RandomUtil;

/**
 * @author : DengYing
 * @CreateDate : 2018年8月14日 下午3:05:59
 * @Description ：战斗抽象
 */
public abstract class AbstractFight {
	
	static final Log log = LogFactory.get(AbstractFight.class);
	
	/** 战斗ID */
	private String fightId;
	/** 战斗奖励 */
	private Map<Integer, Integer> rewards;
	/** 战斗处理器 */
	private FightHandler handler;
	/** 助战英雄 */
	private HeroInfo helpHero;
	/** 助战玩家id */
	private int helpPid;
	/** 随机种子 */
	private int randomSeed;
	/** 是否胜利 */
	private boolean isWin;
	/** 开始时间 */
	private Date startDate;
	/** 扩展属性 */
	private Map<String, Object> attr = Maps.newHashMap();
	
	/** 构建战斗 */
	public AbstractFight build(){
		fightId = RandomUtil.randomUUID();

		randomSeed = RandomUtil.randomInt();
		
		if(handler==null){
			log.warn("构建的战斗，没有战斗处理器");
		}
		return this;
	}	
	
	/** 战斗开始 */
	public void start(){
		if (handler!=null && handler.startCheck(this)) {
			handler.fightStart(this);
			this.startDate = new Date();
			FightManager.me().createFight(this);
		}
	}
	
	/** 战斗结束 */
	public void over(boolean isWin){
		if (FightManager.me().contains(this)) {
			this.isWin = isWin;
			
			if (handler!=null && handler.overCheck(this)) {
				if (this.isWin) {
					handler.fightWin(this);
				}else {
					handler.fightFail(this);
				}
				handler.fightOver(this);
				FightManager.me().destroyFight(this);
			}			
		}
	}
	
	public String getFightId() {
		return fightId;
	}

	public Map<Integer, Integer> getRewards() {
		return rewards;
	}

	public AbstractFight setRewards(Map<Integer, Integer> rewards) {
		this.rewards = rewards;
		return this;
	}

	public AbstractFight setHandler(FightHandler handler) {
		this.handler = handler;
		return this;
	}

	public AbstractFight setHelpHero(HeroInfo helpHero) {
		this.helpHero = helpHero;
		return this;
	}

	public FightHandler getHandler() {
		return handler;
	}

	public HeroInfo getHelpHero() {
		return helpHero;
	}

	public int getRandomSeed() {
		return randomSeed;
	}

	public boolean isWin() {
		return isWin;
	}

	public Date getStartDate() {
		return startDate;
	}

	/**
	 * 取得属性
	 * 
	 * @param key
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public <T> T getAttr(String key){
		return (T) attr.get(key);
	}
	
	/**
	 * 取得属性
	 * 
	 * @param key
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public <T> T getAttr(String key,T defaultValue){
		if (attr.containsKey(key)) {
			return (T) attr.get(key);
		}else {
			return defaultValue;
		}
	}

	/**
	 * 设置属性
	 * 
	 * @param key
	 * @param value
	 * @return 
	 */
	public AbstractFight setAttr(String key, Object value){
		attr.put(key, value);
		return this;
	}

	/**
	 * 删除属性
	 * 
	 * @param key
	 * @return 
	 */
	public AbstractFight removeAttr(String key){
		attr.remove(key);
		return this;
	}
	
	/**
	 * 清空属性
	 * 
	 * @param key
	 * @return 
	 */
	public AbstractFight clearAttr(){
		attr.clear();
		return this;
	}

	public int getHelpPid() {
		return helpPid;
	}

	public AbstractFight setHelpPid(int helpPid) {
		this.helpPid = helpPid;
		return this;
	}
}
